package net.llz.component.impl;

import lombok.extern.slf4j.Slf4j;
import net.llz.component.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MailServiceImpl implements MailService {

    /**
     * Spring Boot 提供了一个发送邮件的简单抽象，直接注入即可使用
     */
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.from}")
    private String from;
    /**
     * 发送邮件
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendMail(String to, String subject, String content) {
        //创建邮箱消息对象
        SimpleMailMessage message = new SimpleMailMessage();
        //配置邮箱发件人
        message.setFrom(from);
        //邮箱收件人
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
        log.info("邮箱发送成功:",message.toString());
    }
}
